﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Oragon.Architecture.Data.SqlBuilder
{
	public enum TopUnit
	{
		Records,
		Percent
	}

	/// <summary>
	/// Representa a cláusula Top de um Select SQL
	/// </summary>
	[Serializable]
	public class TopClause : ISQLStatement
	{
		public int Quantity { get; set; }
		public TopUnit Unit { get; set; }

		public TopClause() : this(100, TopUnit.Percent) { }

		public TopClause(int quantity)
		{
			this.Quantity = quantity;
			this.Unit = TopUnit.Records;
		}

		public TopClause(int quantity, TopUnit unit)
		{
			this.Quantity = quantity;
			this.Unit = unit;
		}


		#region ISQLStatement Members

		public string ToSQL()
		{
			if ((this.Quantity == 100) && (this.Unit == TopUnit.Percent))
				return string.Empty;
			else
			{
				if (this.Unit == TopUnit.Percent)
					return string.Format(System.Globalization.CultureInfo.InvariantCulture, "TOP {0} PERCENT", this.Quantity);
				else
					return string.Format(System.Globalization.CultureInfo.InvariantCulture, "TOP {0}", this.Quantity);
			}
		}

		#endregion
	}
}
